package com.sdtv.basemodule.config;

import android.app.Application;

import com.alibaba.android.arouter.launcher.ARouter;
import com.billy.android.loading.Gloading;
import com.blankj.utilcode.util.LogUtils;
import com.sdtv.basemodule.BuildConfig;
import com.sdtv.basemodule.config.life.ActivityLifecycleCallBack;
import com.sdtv.basemodule.image.ImageLoaderDelegate;
import com.sdtv.basemodule.rx.errorhandler.RxErrorHandler;

/**
 * @ProjectName: MediunFusion
 * @Package: com.sdtv.basemodule.config
 * @ClassName: AppConfig
 * @Description: 枚举类型 -里面可以自定义方法 且方法君威
 * @Author: 小郭同学
 * @CreateDate: 2019-06-20 09:43
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public enum AppConfig {
    //枚举对象
    INSTANCE;

    private AppSetting mAppSetting;

    private AppHttpSetting httpSetting;

    private Application application;

    private BaseConfig config;

    //错误处理
    private RxErrorHandler rxErrorHandler;

    /**
     * 初始化应用的基本配置
     */
    public void initConfig(BaseConfig config) {
        mAppSetting = config.getAppSetting();
        //设置GloadingAdapter 状态设置
        if (null != mAppSetting.getGloadingAdapter()) {
            Gloading.initDefault(mAppSetting.getGloadingAdapter());
            //设置为debug模式
            Gloading.debug(true);
        }
        this.application = mAppSetting.getApplication();
        httpSetting = mAppSetting.getAppHttpSetting();
        this.config = config;
        //注册生命周期
        ActivityLifecycleCallBack.getInstance().init(application);
        // 设置图片加载框架 -- 默认使用Glide加载
        ImageLoaderDelegate.init(config.getImageProcessor(), config.getImageConfig());
        // 加载路由框架
        initARouter(application);

    }


    /**
     * 路由框架
     */
    private void initARouter(Application application) {
        if (BuildConfig.DEBUG) {
            //打印日志
            ARouter.openLog();
            //开启调试模式(如果在InstantRun模式下运行，必须开启调试模式！
            // 线上版本需要关闭,否则有安全风险)
            ARouter.openDebug();
        } else {
            //关闭LogUtils日志打印
            LogUtils.getConfig().setLogSwitch(true);
        }
        //推荐在Application中初始化
        ARouter.init(application);

    }

    public AppSetting getmAppSetting() {
        return mAppSetting;
    }

    public AppHttpSetting getHttpSetting() {
        return httpSetting;
    }

    public Application getApplication() {
        return application;
    }


    public RxErrorHandler getRxErrorHandler() {
        return RxErrorHandler.builder()
                .with(mAppSetting.getApplication())
                .setErrorListener(mAppSetting.getErrorListener())
                .build();
    }

    public void setRxErrorHandler(RxErrorHandler rxErrorHandler) {
        this.rxErrorHandler = rxErrorHandler;
    }

    public BaseConfig getConfig() {
        return config;
    }


}

